<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="inet.db.tennis.TennisSeason"%>
<%@page import="inet.db.tennis.TennisPhase"%>
<%@page import="inet.util.StringTool"%>
<%@page import="inet.db.tennis.TennisMatch"%>
<%@page import="inet.db.tennis.buffer.TennisBuffer"%>
<%@page import="inet.db.tennis.TennisPlayer"%>
<%@page import="inet.db.tennis.TennisMatchGame"%>
<jsp:useBean id="tennisMatchDAO" class="inet.db.tennis.TennisMatchDAO" scope="session"/>
<jsp:useBean id="tennisMatchGameDAO" class="inet.db.tennis.TennisMatchGameDAO" scope="session"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Tường thuật trận đấu</title>
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/reset.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/text.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/grid.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/layout.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/nav.css" media="screen" />
    <!--[if IE 6]><link rel="stylesheet" type="text/css" href="css/ie6.css" media="screen" /><![endif]-->
    <!--[if IE 7]><link rel="stylesheet" type="text/css" href="css/ie.css" media="screen" /><![endif]-->
    <link href="<%= request.getContextPath() %>/admin/tennis/css/table/demo_page.css" rel="stylesheet" type="text/css" />
    <link href="<%= request.getContextPath() %>/admin/tennis/css/style.css" rel="stylesheet" type="text/css" />
    <!-- BEGIN: load jquery -->
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-1.6.4.min.js" type="text/javascript"></script>
    <script type="<%= request.getContextPath() %>/admin/tennis/text/javascript" src="js/jquery-ui/jquery.ui.core.min.js"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.widget.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.accordion.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.effects.core.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.effects.slide.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.mouse.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.sortable.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/table/jquery.dataTables.min.js" type="text/javascript"></script>
    <!-- END: load jquery -->
    <script type="text/javascript" src="<%= request.getContextPath() %>/admin/tennis/js/table/table.js"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/setup.js" type="text/javascript"></script>
    <script type="text/javascript">
    	function f_delete_select_item() {
    		document.formListPhase.action.value = 'delete_selected';
    		document.formListPhase.submit();
    	}
    </script>
</head>
<body>
    <div class="container_12">
        <%@ include file="../include/header.jsp" %>
        <% 
        	response.setCharacterEncoding("utf-8");
        	request.setCharacterEncoding("utf-8");
        	String sId = request.getParameter("id");
        	if( sId == null || !StringTool.isNumberic(sId)) {
        		sId = "0";
        	}
        	String sAction = request.getParameter("action");
        	TennisMatch match = null;
        	int TYPE_GAME = 0;
        	int TYPE_SET = 1;
        	int TYPE_TIA_BREAK = 2;
        	
        	int SPECIAL_POINT_DEFAULT = 0;
        	int SPECIAL_BREAK_POINT = 1;
        	int SPECIAL_SET_POINT = 2;
        	int SPECIAL_MATCH_POINT = 3;
        	
        	if("UPDATE".equals(sAction)) {
        	   // Tay vợt nào ăn điểm.
        	   String sPlayerScore = request.getParameter("playerScore");
        	   
        	   //match = tennisMatchDAO.getMatchResult(new BigDecimal(sId));
        	   // lấy bản ghi mới nhất trong bảng tennis_match_game của trận đấu có match_id = sId
        	   TennisMatchGame matchGame = tennisMatchGameDAO.getNewestRows( match.getId() );
        	   // Nếu chưa có bản ghi nào ==> event đầu tiên của trận đấu ==> MATCH_SET = 1
        	   int currentSet = 1;
        	   int currentServe = 0;
        	   int gameType = TYPE_GAME; 
        	   int specialPoint = SPECIAL_POINT_DEFAULT;
        	   String point1 = "0";
        	   String point2 = "0";
        	   if (match.getCurrentPlayerServe().equals(match.getFirstPlayerId())){
        	       currentServe = 0; // Tay vợt 1 giao bóng.
        	   } else {
        	       currentServe = 1; // Tay vợt 2 giao bóng.
        	   }
        	   
        	   if(matchGame == null) {
        	       if("1".equals(sPlayerScore)){ // cong điểm cho tay vợt 1
	                   point1 = "15";
	                   point2 = "0";
	               } else { // cộng điểm cho tay vợt 2
	                   point1 = "0";
                       point2 = "15";
	               }
        	   } else {
        	   	   // Truong hop khong phai la event dau
        	   	   if("1".equals(sPlayerScore)){ // cong điểm cho tay vợt 1
        	   	   		if("0".equals(matchGame.getPoint1())) point1 = "15";
	                    else if ("15".equals(matchGame.getPoint1())) point1 = "30"; 
	                    else if ("30".equals(matchGame.getPoint1())) point1 = "40";
	                    else if ("40".equals(matchGame.getPoint1()) && "40".equals(matchGame.getPoint2())) point1 = "A";
	                    else if ("A".equals(matchGame.getPoint1())) {
	                    	// Player 1 thắng game đấu.
	                    	point1 = "0";
	                    	point2 = "0";
	                    	// Cộng tỷ số set đấu.
	                    	
	                    }
	               } else { // cộng điểm cho tay vợt 2
	                   point1 = "0";
                       point2 = "15";
	               }
        	   }
        	   // Ghi vao bang TENNIS_MATCH_GAME
        	   //BigDecimal matchId, int matchSet, String point1, String point2, int serve, int type, int specialPoint
        	   tennisMatchGameDAO.insertRow(match.getId(), currentSet, point1, point2, currentServe, gameType, specialPoint);
        	   // Nếu có bản ghi ==> lấy đc SET hiện tại, 
        	   
        	}
        	
        	//match = tennisMatchDAO.getMatchResult(new BigDecimal(sId));
        	match = tennisMatchDAO.getRow(new BigDecimal(sId));
        %>
        <div class="grid_12">
        	<%
               	if(match != null && match.getFirstPlayerId() != null && match.getSecondPlayerId() != null) {
               	    TennisPlayer player1 = (TennisPlayer) TennisBuffer.getPlayerFromId(match.getFirstPlayerId());
               	    if(player1 == null){
               	        player1 = TennisBuffer.reloadPlayer(match.getFirstPlayerId());
               	    }
                    TennisPlayer player2 = (TennisPlayer) TennisBuffer.getPlayerFromId(match.getSecondPlayerId());
                    if(player2 == null){
                        player2 = TennisBuffer.reloadPlayer(match.getSecondPlayerId());
                    }
                    if(player1 == null || player2 == null) {
                        out.println("Không tìm thấy thông tin tay vợt");
                        return;
                    }
           	%>
           	<div class="box round first grid">
                <h2>Tường thuật trận đấu</h2>
                <div class="block">
                	<form name="formListPhase" method="post">
                		<input type="hidden" name="action"/>
                		<input type="hidden" name="playerScore"/>
	                    <table class="data display datatable" id="example">
							<tbody>
								<tr class="eventt">
									<td align="right" style="width: 30%"><%= player1 != null ? player1.getName():"" %></td>
									<td>&nbsp;</td>
									<td style="width: 30%"><%= player2 != null ? player2.getName():"" %></td>
								</tr>
								<tr class="eventt">
									<td align="right">
										[ + ]
									</td>
									<td>
										<table class="data display datatable" id="example">
										    <% 
										      if(match.getTotalSet() >= 3) {
										    %>
											<tr>
												<td class="eventt">
												    <span id="firstTiaBreakSet1Score_<%= match.getId()%>"><%= match.getFirstTieBreakSet1Score() != 0 ? match.getFirstTieBreakSet1Score():"" %></span>
												</td>
												<td class="eventt">
												    <span id="firstSet1Score_<%= match.getId()%>"><%= match.getFirstSet1Score() != 0 ? match.getFirstSet1Score():"" %></span>
												</td>
												<td class="eventt">
												    <span id="secondSet1Score_<%= match.getId()%>"><%= match.getSecondSet1Score() != 0 ? match.getSecondSet1Score():"" %></span>
												</td>
												<td class="eventt">
												    <span id="secondTiaBreakSet1Score_<%= match.getId()%>"><%= match.getSecondTieBreakSet1Score() != 0 ? match.getSecondTieBreakSet1Score():"" %></span>
												</td>
											</tr>
											<tr>
												<td class="eventt">
                                                    <span id="firstTiaBreakSet2Score_<%= match.getId()%>"><%= match.getFirstTieBreakSet2Score() != 0 ? match.getFirstTieBreakSet2Score():"" %></span>
                                                </td>
                                                <td class="eventt">
                                                    <span id="firstSet2Score_<%= match.getId()%>"><%= match.getFirstSet2Score() != 0 ? match.getFirstSet2Score():"" %></span>
                                                </td>
                                                <td class="eventt">
                                                    <span id="secondSet2Score_<%= match.getId()%>"><%= match.getSecondSet2Score() != 0 ? match.getSecondSet2Score():"" %></span>
                                                </td>
                                                <td class="eventt">
                                                    <span id="secondTiaBreakSet2Score_<%= match.getId()%>"><%= match.getSecondTieBreakSet2Score() != 0 ? match.getSecondTieBreakSet2Score():"" %></span>
                                                </td>
											</tr>
											<tr>
												<td class="eventt">
                                                    <span id="firstTiaBreakSet3Score_<%= match.getId()%>"><%= match.getFirstTieBreakSet3Score() != 0 ? match.getFirstTieBreakSet3Score():"" %></span>
                                                </td>
                                                <td class="eventt">
                                                    <span id="firstSet3Score_<%= match.getId()%>"><%= match.getFirstSet3Score() != 0 ? match.getFirstSet3Score():"" %></span>
                                                </td>
                                                <td class="eventt">
                                                    <span id="secondSet3Score_<%= match.getId()%>"><%= match.getSecondSet3Score() != 0 ? match.getSecondSet3Score():"" %></span>
                                                </td>
                                                <td class="eventt">
                                                    <span id="secondTiaBreakSet3Score_<%= match.getId()%>"><%= match.getSecondTieBreakSet3Score() != 0 ? match.getSecondTieBreakSet3Score():"" %></span>
                                                </td>
											</tr>
											<% 
											  }
											  if(match.getTotalSet() == 5) {
											%>
											<tr>
                                                <td class="eventt">&nbsp;</td>
                                                <td class="eventt">&nbsp;</td>
                                                <td class="eventt">&nbsp;</td>
                                                <td class="eventt">&nbsp;</td>
                                            </tr>
                                            <tr>
                                                <td class="eventt">&nbsp;</td>
                                                <td class="eventt">&nbsp;</td>
                                                <td class="eventt">&nbsp;</td>
                                                <td class="eventt">&nbsp;</td>
                                            </tr>
											<%
											  }
											%>
										</table>
									</td>
									<td>
										[ + ]
									</td>
								</tr>
							</tbody>
						</table>
					</form>
                </div>
            </div>
            <% 
            	} else {
            %>
            <div class="box round first grid">
                <h2>Tường thuật trận đấu</h2>
                <div class="block">
                	<div class="message warning">
                        <h5>Cảnh báo !</h5>
                        <p>
                            Không tồn tại trận đấu có Id = <%= sId %>
                        </p>
                    </div>
              	</div>
            </div>
            <%
            	}
            %>
        </div>
        <div class="clear">
        </div>
    </div>
    <%@ include file="../include/footer.jsp" %>
</body>
</html>